#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<vector>
#include<set>
using namespace std;


class Solution {
public:
    bool wordPattern(string pattern, string s) {
        map<char,string> M;
        //先把s中的字符按空格分割出来
        vector<string> tempS;
        string temp = "";
        for(int i=0;i<s.size();i++){
            if(s[i]!=' '){
                temp+=s[i];
            }
            else{
                tempS.push_back(temp);
                temp = "";
            }
        }
        tempS.push_back(temp);
        if(pattern.size()!=tempS.size()){
            return false;
        }
        set<string> V;
        for(int i = 0;i<pattern.size();i++){
            if(M[pattern[i]] == ""){
                M[pattern[i]] = tempS[i];
                if(V.insert(tempS[i]).second == false){
                    return false;
                }
            }
            else{
                if(M[pattern[i]]!=tempS[i]){
                    return false;
                }
            }
        }

        return true;
    }
};


int main(){
    Solution S;
    string pattern = "abba";
    string str = "dog cat cat dog";
    cout<<S.wordPattern(pattern,str)<<endl;
}